{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Basics of Python: Loops </h2>\n",
    "\n",
    "We review using loops in python here. \n",
    "\n",
    "Please run each cell and check the results."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> For-loop </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's print all numbers between 0 and 9\n",
    "for i in range(10): print(i)\n",
    "# range(n) represents the list of all numbers from 0 to n-1\n",
    "# i is the variable to take the values in the range(n) iteratively: 0, 1, ..., 9 in our example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's write the same code in two lines\n",
    "for i in range(10): # do not forget to use colon\n",
    "    print(i)\n",
    "    # the second line is indented\n",
    "    # that means the command in the second line will be executed inside the for-loop\n",
    "    # any other code executed inside the for-loop must be intented in the same way\n",
    "    #my_code_inside_for-loop_2\n",
    "    #my_code_inside_for-loop_3\n",
    "    #my_code_inside_for-loop_4\n",
    "# now I am out of the scope of for-loop\n",
    "#my_code_outside_for-loop_1\n",
    "#my_code_outside_for-loop_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's calculate the summation 1+2+...+10 by using a for-loop\n",
    "\n",
    "# we use variable total for the total summation\n",
    "total = 0 \n",
    "for i in range(11): # do not forget to use colon\n",
    "    total = total + i # total is updated by i in each iteration\n",
    "    # alternatively the same assignment can shortly be written as total =+ i similar to languages C, C++, Java, etc.\n",
    "# now I am out of the scope of for-loop\n",
    "# let's print the latest value of total\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's calculate the summation 10+12+14+...+44\n",
    "# we create a list having all numbers in the summation\n",
    "# for this purpose, this time we will use three parameters in range\n",
    "total = 0\n",
    "for j in range(10,45,2): # the range is defined between 10 and 44, and the value of j will be increased by 2 after each iteration\n",
    "    total += j # let's use the shorten version of total = total + j\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's calculate the summation 1+2+4+8+16+...+256\n",
    "# remark that 256 = 2*2*...*2 (8 times)\n",
    "total = 0\n",
    "current_number = 1 # this value will be multiplied by 2 after each iteration\n",
    "for k in range(9):\n",
    "    total = total + current_number # current_number is 1 at the beginning, and its value will be doubled after each iteration\n",
    "    current_number = 2 * current_number # let's double the value of the current_number for the next iteration\n",
    "    # short version of the same assignment: current_number *= 2 as in the languages C, C++, Java, etc.\n",
    "# now I am out of the scope of for-loop\n",
    "# let's print the latest value of total\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# instead of a range, we can directly use a list\n",
    "for i in [1,10,100,1000,10000]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# instead of [...], we can also use (...)\n",
    "# but this time it is a tuple, not a list (keep in your mind that the values in a tuple cannot be changed)\n",
    "for i in (1,10,100,1000,10000):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's create a range containing the multiples of 7 and between 10 and 91\n",
    "for j in range(14,92,7): \n",
    "    # 14 is the first multiple of 7 greater or equal to 10\n",
    "    # 91 should be in the range, and so we write 92\n",
    "    print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's create a range between 11 and 22\n",
    "for i in range(11,23):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can also use variables in range\n",
    "n = 5\n",
    "for j in range(n,2*n): print(j) # we will print all numbers in {n,n+1,n+2,...,2n-1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can use a list of strings\n",
    "for name in (\"Asja\",\"Balvis\",\"Fyodor\"):\n",
    "    print(\"Hello\",name,\":-)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# any range can be converted to a list\n",
    "L1 = list(range(10))\n",
    "print(L1)\n",
    "\n",
    "L2 = list(range(55,200,11))\n",
    "print(L2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Calculate the value of summation $ 3+6+9+\\cdots+51 $, and the print the result.\n",
    "\n",
    "You should see 459 as the outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B10_Python_Basics_Loops_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "$ 3^k $ means $ 3 \\cdot 3 \\cdot \\cdots \\cdot 3 $ ($ k $ times) for $ k \\geq 2 $. \n",
    "\n",
    "Moreover, $ 3^0  $ is 1 and $ 3^1 = 3 $.\n",
    "\n",
    "Calculate the value of summation $ 3^0 + 3^1 + 3^2 + \\cdots + 3^8 $, and then print the result.\n",
    "\n",
    "You should see 9841 as the outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B10_Python_Basics_Loops_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> While-loop </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's calculate the summation 1+2+4+8+...+256 by using a while-loop\n",
    "total = 0\n",
    "i = 1\n",
    "\n",
    "#while condition(s):\n",
    "#    your_code1\n",
    "#    your_code2\n",
    "#    your_code3\n",
    "while i < 257: # this loop iterates as long as i is less than 257\n",
    "    total = total + i\n",
    "    i = i * 2 # i is doubled in each iteration, and so soon it will be greater than 256\n",
    "    \n",
    "print(total)\n",
    "# remember that we calculated this summation as 511 before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = [0,1,2,3,4,5,11] # this is a list containing 7 integer values\n",
    "i = 0\n",
    "while i in L: # this loop will be iterated as long as i is in L\n",
    "    print(i)\n",
    "    i = i + 1 # the value of i iteratively increased, and so soon it will hit a value not in the list L\n",
    "    \n",
    "# the loop is terminated after i is set to 6, because 6 is not in L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's use negation in the condition of while-loop\n",
    "L = [10] # this list has a single element\n",
    "i = 0\n",
    "while i not in L: # this loop will be iterated as long as i is not equal to 10\n",
    "    print(i)\n",
    "    i = i+1 # the value of i will hit to 10 after ten iterations\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's rewrite the same loop by using a direct inequality\n",
    "i = 0\n",
    "while i != 10: # \"!=\" is used for \"not equal to\" operator \n",
    "    print(i) \n",
    "    i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's rewrite the same loop by using negation of equality\n",
    "i = 0\n",
    "while not (i == 10): # \"==\" is used for \"equal to\" operator \n",
    "    print(i) \n",
    "    i=i+1\n",
    "    \n",
    "# while-loop seems having more fun :-)\n",
    "# but we should be more careful when writing the condition(s)!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the summation $ S(n) =  1+ 2+ 3 + \\cdots + n $ for some natural number $ n $.\n",
    "\n",
    "Let's find the minimum value of $ n $ such that $ S(n) \\geq 1000 $.\n",
    "\n",
    "While-loop works very well for this task.\n",
    "<ul>\n",
    "    <li>We can iteratively increase $ n $ and update the value of $ S(n) $.</li>\n",
    "    <li>The loop iterates as long as $S(n)$ is less than 1000.</li>\n",
    "    <li>Once it hits 1000 or a bigger number, the loop will be terminated.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# summation and n is zero at the beginning\n",
    "S = 0\n",
    "n = 0\n",
    "while S < 1000: # this loop will stop after S exceeds 999 (S = 1000 or S > 1000)\n",
    "    n = n +1\n",
    "    S = S + n\n",
    "# let's print n and S\n",
    "print(n,S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 3 </h3>\n",
    "\n",
    "Consider the summation $ T(n) = 1 + \\dfrac{1}{2} + \\dfrac{1}{4}+ \\dfrac{1}{8} + \\cdots + \\dfrac{1}{2^n} $ for some natural number $ n $. \n",
    "\n",
    "Remark that $ T(0) = \\dfrac{1}{2^0} = \\dfrac{1}{1} = 1 $.\n",
    "\n",
    "This summation can be arbitrarily close to $2$. \n",
    "\n",
    "Let's find the minimum value of $ n $ such that the closeness of $ T(n) $ to $2$ is less than $ 0.01 $.\n",
    "\n",
    "In other words, let's find the minimum value of $n$ such that $ T(n) > 1.99 $.\n",
    "\n",
    "The operator for \"less than or equal to\" in python is \"$ < = $\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# three examples for the operator \"less than or equal to\"\n",
    "#print (4 <= 5)\n",
    "#print (5 <= 5)\n",
    "#print (6 <= 5)\n",
    "# you may comment out the above three lines for executing and seeing the results\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B10_Python_Basics_Loops_Solutions.ipynb#task3\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 4 </h3>\n",
    "\n",
    "The task is to pick numbers between 0 and 9 randomly until hitting the number 3, and then print the number of attempt(s).\n",
    "\n",
    "We can use <i>randrange</i> function from <i>random</i> module for randomly picking a number in a given range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is the code for including function randrange to our program \n",
    "from random import randrange\n",
    "# randrange(n) picks a number from the list [0,1,2,...,n-1] randomly\n",
    "#r = randrange(100)\n",
    "#print(r)\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B10_Python_Basics_Loops_Solutions.ipynb#task4\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 5 </h3>\n",
    "\n",
    "This task is for challenge. \n",
    "\n",
    "It is designed for the usage of double nested loops: one loop is inside of the other loop.\n",
    "\n",
    "In the fourth task above, the expected number of attempt(s) to hit number 3 is 10. \n",
    "\n",
    "Let's do a series of experiment by using your solution for Task 4.\n",
    "\n",
    "Experiment 1: Execute your code 20 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 2: Execute your code 200 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 3: Execute your code 2000 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 4: Execute your code 20000 times, and then calculate the average attempts.\n",
    "\n",
    "Experiment 5: Execute your code 200000 times, and then calculate the average attempts.\n",
    "\n",
    "<i>Your experimental average should get closer to 10 when the number of executions is increased.</i>\n",
    "\n",
    "Remark that all five experiments can be automatically done by using triple loops."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B10_Python_Basics_Loops_Solutions.ipynb#task5\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# here is a schematic example for double nested loops\n",
    "#for i in range(10):\n",
    "#    your_code1\n",
    "#    your_code2\n",
    "#    while j != 7:\n",
    "#        your_code_3\n",
    "#        your_code_4\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
